package com.example.neusoft.zyfypt_wk.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.zyfypt_wk.R;
import com.example.neusoft.zyfypt_wk.adapter.ArticleAdapter;
import com.example.neusoft.zyfypt_wk.adapter.ProjectAdapter;
import com.example.neusoft.zyfypt_wk.bean.ArticleBean;
import com.example.neusoft.zyfypt_wk.bean.ProjectBean;
import com.example.neusoft.zyfypt_wk.iface.ArticleListener;
import com.example.neusoft.zyfypt_wk.iface.ProjectListener;
import com.example.neusoft.zyfypt_wk.model.ArticleModel;
import com.example.neusoft.zyfypt_wk.model.ProjectModel;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Neusoft on 2017/4/9.
 */
/*案例——项目*/
public class Fragment42 extends Fragment {
    private  TextView tvinfo;
    private View view=null;
    private Context context;

    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager  layoutManager;//显示布局效果
    private ProjectAdapter adapter;//适配器
    private List<ProjectBean> list=null;//数据源

    private SharedPreferences sp;
    private String sessionID="";

    ProjectListener listener=new ProjectListener() {
        @Override
        public void onResponse(List<ProjectBean> beanlist) {
            list=beanlist;//得到网络数据
            adapter.setList(list);//传给adapter
            adapter.notifyDataSetChanged();//通知更新
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
       //动态加载Fragment1的布局文件
        view=inflater.inflate(R.layout.fragment42,container,false);
        sp=context.getSharedPreferences("login",MODE_PRIVATE);

        readSP();



        //实例化ArticleModel，调用方法获取网络数据
        ProjectModel model=new ProjectModel();
        model.getResultList(
                "project",
                1,
                sessionID,
                listener);

        initRecyclerView();
        //返回动态生成的view
        return view;
    }

    private void readSP() {
        sessionID=sp.getString("sessionID",null);

    }
    private void initRecyclerView() {
        //获取RecyclerView，设置属性，获取数据源，绑定
        recyclerView=(RecyclerView)view.findViewById(R.id.recyclerview);
        //创建默认的线性布局
        layoutManager=new LinearLayoutManager(context);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item高度，提高性能
        recyclerView.setHasFixedSize(true);
        //获取数据并创建Adaper
        //list=getData();
        adapter =new ProjectAdapter(context);
        adapter.setList(list);
        //绑定RecyclerView和adapter
        recyclerView.setAdapter(adapter);


    }
}
